﻿define(['app'], function (app) {
    app.controller('borrowerEditController', ['$scope', '$timeout', '$http', '$q', '$state', '$stateParams', 'borrowerService',
       function ($scope, $timeout, $http, $q, $state, $stateParams, borrowerService) {

           //代码书写步骤
           //1.用【$scope】定义公共类model,和页面绑定的model
           //2.用【var】定义controller内部用的变量
           //3.用【$scope】和【var】定义方法，注意区分两者定义的方法的区别
           //4.最后定义【init】初始化方法，并调用，进行页面的初始化加载
           //定义规则，按可用性分忧优先级，公共的在前，private的在后

           /**
            * 查询tableList数据
            * @return {[object]} [返回table列表数据]
            */

           $scope.model = {};
           $scope.id = $stateParams.id;

           var Init = function () {
               if ($scope.id == null || $scope.id == undefined || $scope.id == "") {
                   $scope.model = {};
                   $scope.model.gender = 2;
                   $scope.model.record = 1;
                   return;
               }
               borrowerService.get($scope.id).then(
                     function (response) {
                         if (response.status == 200) {
                             $scope.model = response.data;

                         } else {
                             console.log(response);
                             alert("查询数据失败!");
                         }
                     },
                     function (code) {
                         alert("系统出现问题了！");
                     });
           };
           $scope.back = function () {
               history.back();
           };

           $scope.save = function () {
               if ($scope.model.id == null || $scope.model.id == "") {

                   borrowerService.save($scope.model).then(
                         function (response) {
                             if (response.status == 500) {
                                 alert(response.message);
                             }
                             else {
                                 history.back();
                             }
                         },
                         function (code) {
                             alert("系统出现问题了！");
                         });
               } else {
                   borrowerService.update($scope.model).then(
                         function (response) {
                             //if (response.success) {
                             history.back();
                             //} else {
                             //    console.log(response);
                             //alert("保存数据失败!");
                             //}
                         },
                         function (code) {
                             alert("系统出现问题了！");
                         });

               }

           };

           Init();
       }]);
});